<?php

namespace App\Http\Controllers\Admin\Service;

use App\Http\Controllers\Controller as BaseController;
use App\Models\Orders\OrdersUserModel;
use App\Repository\Orders\OrdersLabelRepository;
use App\Repository\Service\ServiceCallRepository;
use App\Repository\Service\ServiceCallTaskRepository;
use App\Repository\TaggingConfig\TaggingConfigRepository;
use App\Repository\UploadRepository;
use Illuminate\Http\Request;

// 客服外呼任务模块
class CallTaskController extends BaseController
{
    private $repoist;

    public function __construct(ServiceCallTaskRepository $repository)
    {
        $this->repoist = $repository;
    }

    public function index(Request $request)
    {
        $data = $this->repoist->index($request->all(), $request->get('pageNum'), $request->get('pageSize'));
        if ($data['code'] == 200) {
            $ModelOrdersUser = new OrdersUserModel();
            $Repository = new OrdersLabelRepository();
            $data['weightTypeList'] = config('card.serviceTaskWeightType');
            $data['ordersStatusList'] = config('card.ordersStatus');
            $data['sourceTypeList'] = config('card.sourceType');
            $data['activatStatusList'] = config('card.activatStatus');
            $data['firstStatusList'] = config('card.firstStatus');
            $data['statusList'] = config('card.serviceCallTaskStatus');
            $data['listTaggingList'] = $ModelOrdersUser->getTagging();
            $data['firstMoneyList'] = config('card.firstMoneyList');
            $data['isOnlineStatusList'] = config('card.isOnlineStatus');
//            意向标签
            $data['labelList'] = $Repository->dataList();
            $data['callStatusList'] = config('card.serviceCallStatus');

        }
        return $data;
    }

//外呼界面要的一些选项信息,与订单信息里的一致
    public function callTaskOptionsList(Request $request)
    {
        $TaggingRepository = new TaggingConfigRepository();
        $Repository = new OrdersLabelRepository();
        $data = [];
        $data['expressStatusList'] = config('express.status');
        $data['expressCompanyList'] = config('express.company');
        $data['reivewStatusList'] = config('card.reviewStatus');
        $data['activatStatusList'] = config('card.activatStatus');
        $data['firstStatusList'] = config('card.firstStatus');
        $data['isOnlineStatusList'] = config('card.isOnlineStatus');
        $data['sourceTypeList'] = config('card.sourceType');
        $data['ordersStatusList'] = config('card.ordersStatus');
        $data['sendMsgStatusList'] = config('card.serviceCallSendMsgStatus');
        $data['smsSendStatusList'] = config('card.smsSendLogsStatus');
        $data['callStatusList'] = config('card.serviceCallStatus');
        $data['forbidChangeStatusList'] = config('card.forbidChangeStatus');
        $data['listTaggingList'] = $TaggingRepository->getTagging();
        $data['labelList'] = $Repository->dataList();

        return $this->returnMsg(200, '已查询', $data);
    }

// 使用短信模板发送短信
    public function sendTplSms(Request $request)
    {
        return $this->repoist->sendTplSms($request->all());
    }

//    外呼大厅
    public function hall(Request $request)
    {
        $data = $this->repoist->hall($request->all(), $request->get('pageNum'), $request->get('pageSize'));
        if ($data['code'] == 200) {
            $data['weightTypeList'] = config('card.serviceTaskWeightType');
            $data['ordersStatusList'] = config('card.ordersStatus');
            $data['sourceTypeList'] = config('card.sourceType');
            $data['activatStatusList'] = config('card.activatStatus');
            $data['statusList'] = config('card.serviceCallTaskStatus');
        }
        return $data;
    }

//加载外呼任务
    public function getCallTaskOrder(Request $request)
    {
        $getFlag = $request->get('getFlag');
//        实时标注任务在拨打
        if ($getFlag == 1) {
            return $this->repoist->setCallFlag($request->all());
        } else if ($getFlag == 2) {
            $Repository = new ServiceCallRepository();
            return $Repository->index($request->all());
        } else if ($getFlag == 10) {
//            保存外呼结果
            $Repository = new ServiceCallRepository();
            return $Repository->saveCallResult($request->all());
        } else {
//        拉一个任务单给前端拨打
            $id = $request->get('id');
            return $this->repoist->getCallTaskOrder($id);
        }
    }

    public function importExcel(Request $request)
    {
        $model = new UploadRepository();
        $shopId = $request->user->shop_id ?? 0;
        $res = $model->upload($request, 'file', false, $shopId, 'excel');
        if (empty($res['file_src']) || !file_exists('./' . $res['file_src'])) {
            return [
                'code' => 100,
                'msg' => !empty($res['msg']) ? $res['msg'] : '上传失败',
                'fileName' => ''
            ];
        }
        $postData = $request->all();
        $postData['file_name'] = $res['file_name'];
        $postData['file_src'] = $res['file_src'];

        return $this->repoist->import($postData);
    }

//任务里的订单数据
    public function getCallTaskLogs(Request $request)
    {
        $id = $request->get('id');
        if (empty($id)) {
            return $this->returnMsg(200, '没有任务id数据', []);
        }
        $page = $request->get('pageNum');
        $limit = $request->get('pageSize');
        return $this->repoist->callTaskLogs($id, $page, $limit);
    }


    public function add(Request $request)
    {
        return $this->repoist->add($request->all());
    }

    public function edit(Request $request)
    {
        return $this->repoist->edit($request->all());
    }

    public function del(Request $request, $id)
    {
        return $this->repoist->unifiedDel($id);
    }
}
